using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using PccCommonForC;
using System.Text;

public partial class Pub_Ajax_AjaxAutoComplete_2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Expires = -1;
        Response.ContentType = "text/plain";
        ProcessPage(Request.QueryString["type"]);
        Response.End();
    }
    private void ProcessPage(string type)
    {
        Response.Write(fnSearch());
    }
    private string fnSearch()
    {
        clsWebShop cls = new clsWebShop();
        DataTable dt;

        dt = cls.GetData("select PDName,PDID from Product p inner join Catalogue c  on p.catid=c.catid where PDName like N'%" + Request.QueryString["q"] + "%'  and p.Status<>'D'  " + " and c.KindNo='P' and c.Status<>'D'").Tables[0];
       

        return CreateJsonParameters(dt);

    
    }

    public string CreateJsonParameters(DataTable dt)
    {


        StringBuilder JsonString = new StringBuilder();

        //Exception Handling
        if (dt != null && dt.Rows.Count > 0)
        {

            JsonString.Append("[ ");

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                JsonString.Append("{ ");
                for (int j = 0; j < dt.Columns.Count; j++)
                {
                    if (j < dt.Columns.Count - 1)
                    {
                        JsonString.Append("\"" + dt.Columns[j].ColumnName.ToString() +
                              "\":" + "\"" +
                              dt.Rows[i][j].ToString() + "\",");
                    }
                    else if (j == dt.Columns.Count - 1)
                    {
                        JsonString.Append("\"" +
                           dt.Columns[j].ColumnName.ToString() + "\":" +
                           "\"" + dt.Rows[i][j].ToString() + "\"");
                    }
                }

                /*end Of String*/
                if (i == dt.Rows.Count - 1)
                {
                    JsonString.Append("} ");
                }
                else
                {
                    JsonString.Append("}, ");
                }
            }

            JsonString.Append("]");
            return JsonString.ToString();
        }
        else
        {
            return null;
        }
    }
}
